﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace log4net.extension.sample
{
    class Program
    {
        static void Main(string[] args)
        {
            log4net.Config.XmlConfigurator.Configure(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"));

            //Log4netMongoTest();

            //Log4netMongoEntityTest();

            //Log4netKafkaTest();

            Log4netNSQTest();

            Console.ReadLine();
        }

        public static void Log4netMongoTest()
        {
            ILog log = LogManager.GetLogger("TestLog");

            log.Error("123", new Exception("exception test"));
            log.Error("exception test");

            Task[] array = new Task[2];
            Stopwatch s = new Stopwatch();
            s.Start();

            for (int k = 0; k < 2; k++)
            {
                array[k] = Task.Run(() =>
                {
                    for (int i = 0; i < 10; i++)
                    {
                        log.Debug("Message" + i.ToString());
                    }
                });
            }

            Task.WaitAll(array);


            s.Stop();

            Console.WriteLine("\r\n done:{0}", s.ElapsedMilliseconds);
        }
        public static void Log4netMongoEntityTest()
        {
            ILog log = LogManager.GetLogger("TestEntityLog");

            Console.WriteLine("init ok!");


            Stopwatch s = new Stopwatch();
            s.Start();

            for (int i = 0; i < 10; i++)
            {
                SimpleEntity entity = new SimpleEntity { RequestId = i, Name = "name#" + i.ToString() };
                log.Debug(JsonConvert.SerializeObject(entity));
            }

            s.Stop();

            Console.WriteLine("\r\n done:{0}", s.ElapsedMilliseconds);
        }
        public static void Log4netKafkaTest()
        {
            ILog log = LogManager.GetLogger("TestKafkaLog");

            Stopwatch s = new Stopwatch();
            s.Start();
            List<Task> tasks = new List<Task>();
            for (int k = 0; k < 10; k++)
            {
                tasks.Add(Task.Run(() =>
                {
                    for (int i = 0; i < 100; i++)
                    {
                        log.Debug("Message" + i.ToString() + ":" + Guid.NewGuid().ToString("N"));
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            s.Stop();

            Console.WriteLine("\r\n done:{0}", s.ElapsedMilliseconds);
        }
        public static void Log4netNSQTest()
        {
            ILog log = LogManager.GetLogger("TestNSQLog");

            Stopwatch s = new Stopwatch();
            s.Start();

            List<Task> tasks = new List<Task>();
            for (int k = 0; k < 10; k++)
            {
                tasks.Add(Task.Run(() =>
                {
                    for (int i = 0; i < 100; i++)
                    {
                        log.Debug("Message" + i.ToString() + ":" + Guid.NewGuid().ToString("N"));
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            s.Stop();

            Console.WriteLine("\r\n done:{0}", s.ElapsedMilliseconds);
        }
    }


    public class SimpleEntity
    {
        public int RequestId { get; set; }
        public string Name { get; set; }
    }
}
